package com.zhang.authoritycenter.service.impl;


import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.authoritycenter.common.mapper.auth.OpenAccountMapper;
import com.zhang.authoritycenter.common.model.dao.auth.OpenAccountDO;
import com.zhang.authoritycenter.common.model.request.AddOpenReqDTO;
import com.zhang.authoritycenter.common.properties.BaseProperties;
import com.zhang.authoritycenter.common.util.AesUtil;
import com.zhang.authoritycenter.service.IOpenAccountService;
import com.zhang.infrastructure.model.KingHoodExceptionUtil;
import com.zhang.infrastructure.model.constant.redis.RedisConstant;
import com.zhang.infrastructure.model.token.KingHoodPrincipal;
import com.zhang.infrastructure.util.IdGenUtil;
import com.zhang.redis.util.RedisManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KingHood
 * @since 2023-12-13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OpenAccountServiceImpl extends ServiceImpl<OpenAccountMapper, OpenAccountDO> implements IOpenAccountService {

    private final RedisManager redisManager;
    private final BaseProperties baseProperties;

    @Override
    public OpenAccountDO checkLogin(String appId, String appSecret) {
        // 查找用户信息
        String key = RedisConstant.OPEN_ACCOUNT_PREFIX+appId;
        Object value = redisManager.keyValue().get(key);
        OpenAccountDO openAccountDO = null;
        if(value instanceof OpenAccountDO ){
            openAccountDO = JSON.parseObject(JSON.toJSONString(value), OpenAccountDO.class);
        }
        if(Objects.isNull(openAccountDO)){
            openAccountDO = new LambdaQueryChainWrapper<>(this.getBaseMapper())
                    .eq(OpenAccountDO::getAppId, appId).one();
            KingHoodExceptionUtil.isNull(openAccountDO,"商户不存在");
            redisManager.keyValue().set(key,openAccountDO,baseProperties.getTableCacheTime(), TimeUnit.SECONDS);
        }
        // 对比数据
        String digestPassword = AesUtil.encode(appId,appSecret);
        KingHoodExceptionUtil.trueThrows(!Objects.equals(openAccountDO.getAppSecret(),digestPassword),"密码错误");

        return openAccountDO;
    }

    @Override
    public Boolean addOpen(AddOpenReqDTO in) {
        OpenAccountDO one = new LambdaQueryChainWrapper<>(this.getBaseMapper())
                .eq(OpenAccountDO::getAppId, in.getAppId()).last("limit 1").one();
        KingHoodExceptionUtil.trueThrows(Objects.nonNull(one),"appId已经存在");

        one = new OpenAccountDO();
        one.setId(IdGenUtil.get().nextId());
        one.setAppId(in.getAppId());
        one.setAppSecret(AesUtil.encode(in.getAppId(),in.getAppSecret()));
        one.setCreateTime(new Date());
        this.save(one);
        return true;
    }
}
